package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.chart.SettlementAllBo;
import org.dromara.system.domain.bo.chart.SettlementListBo;
import org.dromara.system.domain.bo.chart.SettlementMonthBo;
import org.dromara.system.domain.bo.chart.SettlementUnBo;
import org.dromara.system.domain.vo.PayOrderVo;
import org.dromara.system.kernel.OrderSettKernel;
import org.springframework.data.domain.PageImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sett")
public class PaySettController {

    private final OrderSettKernel orderSettKernel;

    /**
     * 查询一个月的未结算订单数量
     *
     * @param bo 查询参数
     */
    @PostMapping("/getUnSettNum")
    public R<?> getUnSettNum(@RequestBody SettlementMonthBo bo) {
        if (bo.getPayTypeId() == null) {
            return R.fail("请先选择支付平台");
        }
        return orderSettKernel.getUnSettNum(bo);
    }

    /**
     * 查询未结算订单列表
     *
     * @param bo 查询参数
     */
    @PostMapping("/getUnSettList")
    public TableDataInfo<?> getUnSettList(@RequestBody SettlementListBo bo) {
        TableDataInfo<PayOrderVo> rspData = new TableDataInfo<>();
        if (bo.getPayTypeId() == null) {
            rspData.setCode(201);
            rspData.setMsg("请先选择支付平台");
            return rspData;
        }
        if (bo.getDateList() == null || bo.getDateList().isEmpty()) {
            rspData.setCode(201);
            rspData.setMsg("请先选择日期");
            return rspData;
        }

        PageImpl<PayOrderVo> list = orderSettKernel.getUnSettList(bo);
        if (LoginHelper.isClient()) {
            rspData.setRows(list.getContent().stream().peek(vo -> {
                if (Objects.equals(vo.getIsFeign(), 1)) {
                    vo.setOrderCid(null);
                }
            }).toList());
        } else {
            rspData.setRows(list.getContent());
        }

        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(list.getTotalElements());
        return rspData;
    }

    /**
     * 查询未结算订单金额、数量
     *
     * @param Bo 查询参数
     */
    @PostMapping("/getUnSettToTal")
    public R<?> getUnSettToTal(@RequestBody SettlementListBo Bo) {
        if (Bo.getPayTypeId() == null) {
            return R.fail("请先选择支付平台");
        }
        if (Bo.getDateList() == null || Bo.getDateList().isEmpty()) {
            return R.fail("请先选择日期");
        }

        Map<String, Object> map = orderSettKernel.getUnSettToTal(Bo);

        return R.ok(map);
    }

    /**
     * 全部结算
     *
     * @param bo 查询参数
     */
    @SaCheckPermission("system:sett:do")
    @PostMapping("/setAllSett")
    public R<?> setAllSett(@RequestBody SettlementAllBo bo) {
        if (bo.getPayTypeId() == null) {
            return R.fail("请先选择支付平台");
        }
        return orderSettKernel.setAllSett(bo);
    }

    /**
     * 设为未结算
     *
     * @param bo 查询参数
     */
    @SaCheckPermission("system:sett:do")
    @PostMapping("/setUnSett")
    public R<?> setUnSett(@RequestBody SettlementUnBo bo) {
        if (bo.getPayTypeId() == null) {
            return R.fail("请先选择支付平台");
        }
        return orderSettKernel.setUnSett(bo);
    }
}
